package leetcode.lt100;

class Solution_54 {

	public static void main(String[] args) {
		int[] nums = { 1, 1, 2, 2, 0, 1, 1 };
		System.out.println(new Solution_54().canJump(nums));
	}

	public boolean canJump(int[] nums) {
		int l = nums.length;

		for (int i = 0; i < l;) {
			// 当前坐标值
			int current = nums[i];
			if (current + i + 1 >= l)
				return true;
			if (current == 0)
				return false;
			// 使下次跳跃最远的距离
			int max = nums[i + 1] + 1;
			// 使下次跳跃最远的坐标
			int maxIndex = i + 1;
			for (int j = 1; j <= current; j++) {
				if (j + nums[i + j] >= max && nums[i + j] != 0) {
					max = j + nums[i + j];
					maxIndex = i + j;
				}
			}
			i = maxIndex;
		}

		return false;
	}
}